package com.gmail.blackdog1987.carcare.widget;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.AbstractWheelTextAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.R;

public class WheelPicker extends Activity implements OnClickListener{
	// Scrolling flag
	private boolean scrolling = false;
	private WheelAdapter adapter;
	private WheelView data;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.wheel_layout);
		
		int resourceid = getIntent().getIntExtra("arrayid", -1);
		String[] arrays = null;
		if(resourceid!=-1){
			arrays = getResources().getStringArray(resourceid);
		}else{
			arrays = getIntent().getStringArrayExtra("array");
		}
		if(arrays==null||arrays.length==0){
			Toast.makeText(this, "unknown error", Toast.LENGTH_LONG).show();
			finish();
			return;
		}
		data = (WheelView) findViewById(R.id.wheel_data);
		View ok = findViewById(R.id.wheel_ok);
		data.setVisibleItems(3);
		adapter = new WheelAdapter(this, arrays);
		data.setViewAdapter(adapter);

		data.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!scrolling) {
					// update ui
				}
			}
		});

		data.addScrollingListener( new OnWheelScrollListener() {
			@Override
			public void onScrollingStarted(WheelView wheel) {
				scrolling = true;
			}
			@Override
			public void onScrollingFinished(WheelView wheel) {
				scrolling = false;
				// update ui
			}
		});

		data.setCurrentItem(1);
		
		ok.setOnClickListener(this);
	}

	/**
	 * Adapter for countries
	 */
	private class WheelAdapter extends AbstractWheelTextAdapter {
		// Countries names
		private String datas[] = new String[] {"USA", "Canada", "Ukraine", "France"};

		/**
		 * Constructor
		 */
		protected WheelAdapter(Context context,String[] datas) {
			super(context, R.layout.wheel_data_text, NO_RESOURCE);
			this.datas = datas;
			setItemTextResource(R.id.wheel_data_text);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
			return view;
		}

		@Override
		public int getItemsCount() {
			return datas.length;
		}

		@Override
		protected CharSequence getItemText(int index) {
			return datas[index];
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.wheel_ok:
			Intent resultData = getIntent();
			resultData.putExtra("resultdata", adapter.getItemText(data.getCurrentItem()));
			setResult(Activity.RESULT_OK, resultData);
			finish();
			break;
		}
	}
}
